#
# Copyright 2008 Yohann R. Coppel
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from pylab import *
import sys

##############################
# Run this script, with as first command line argument, the path to the directory
# where to write the files.
##############################

##############################
package="com.google.devtools.depan.eclipse.cm"
className="ColorMapDef"
##############################

license="""// This file is automatically generated by a script (generateColorMaps.py).
// Do not modify it, or changes will be lost when the script is run again.
/*
 * This file was generated using data provided by the python library matplotlib
 * which is Copyright (c) 2002-2004 John D. Hunter; All Rights Reserved
 *
 * For the rest of the code source, the following copyright and license
 * applies.
 *
 * Copyright 2008 Yohann R. Coppel
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
"""

maps = [m for m in cm.datad.keys() if not m.endswith("_r")]
maps.sort()

print maps

def writeMap(dir, classNme, map):
  f = file(dir+"/"+classNme+".java", "w")
  f.write(license)
  f.write("package %s;\n" % (package))
  f.write("\n")
  f.write("public final class %s {\n" % (classNme))
  f.write("\n")
  f.write("  private %s() { }\n" % (classNme))
  f.write("\n")
  f.write("  public static final float[][][] CM = {\n")
  colors = ["red", "green", "blue"]
  for c in colors:
    f.write("      { // %s\n" % (c))
    for l in cm.datad[map][c][:-1]:
      f.write("        {%sf, %sf, %sf},\n" % (l[0], l[1], l[2]))
    l = cm.datad[map][c][-1]
    f.write("        {%sf, %sf, %sf}},\n" % (l[0], l[1], l[2]))
  f.write("      { // alpha\n")
  f.write("        {0.0f, 1.0f, 1.0f},\n")
  f.write("        {1.0f, 1.0f, 1.0f}}\n")
  f.write("      };\n")
  f.write("}\n")

def writeMapList(dir, classNme, maps):
  f = file(dir+"/"+classNme+".java", "w")
  f.write(license)
  f.write("package %s;\n" % (package))
  f.write("import java.util.HashMap;\n")
  f.write("import java.util.Set;\n")
  f.write("\n")
  f.write("public final class %s {\n" % (classNme))
  f.write("\n")
  f.write("  private %s() { }\n" % (classNme))
  f.write("\n")
  f.write("  private static HashMap<String, float[][][]> map =\n")
  f.write("      new HashMap<String, float[][][]>();\n")
  f.write("  static {\n")
  for m in maps:
    f.write("    map.put(\"%s\", %s.CM);\n" % (m[1], m[0]))
  f.write("  }\n\n")
  f.write("  public static float[][][] getColorMapDef(String name) {\n")
  f.write("    return map.get(name);\n")
  f.write("  }\n\n")
  f.write("  public static Set<String> getColorMapNames() {\n")
  f.write("    return map.keySet();\n")
  f.write("  }\n\n")
  #for m in maps:
  #  f.write("  public static float[][][] MAP_%s = %s.CM;\n" % (m[1], m[0]))
  f.write("}\n")

if __name__ == "__main__":
  dir = sys.argv[1]
  files=[]
  for m in maps:
    changedName = "".join([s.capitalize() for s in m.split("_")])
    klass = className + changedName
    files += [(klass, m)]
    writeMap(dir, klass, m)
  print files

  writeMapList(dir, className, files)

